package com.ioc.business.job.controller;


import cn.hutool.core.util.ObjectUtil;
import com.ioc.business.job.domain.JobTaskDistribution;
import com.ioc.business.job.domain.bo.task.distribution.JobTaskDistributionBo;
import com.ioc.business.job.domain.bo.task.distribution.JobTaskDistributionQueryBo;
import com.ioc.business.job.service.IJobTaskDistributionService;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionImportVo;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionPageVo;
import com.ioc.business.job.vo.task.distribution.JobTaskDistributionVo;
import com.ioc.common.annotation.Log;
import com.ioc.common.core.controller.BaseController;
import com.ioc.common.core.domain.AjaxResult;
import com.ioc.common.core.page.TableDataInfo;
import com.ioc.common.core.page.TableVo;
import com.ioc.common.enums.BusinessType;
import com.ioc.common.utils.poi.ExcelUtil;
import com.ioc.common.validate.AddGroup;
import com.ioc.common.validate.EditGroup;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 作业任务分布表 前端控制器
 * </p>
 *
 * @author WeiZhongRong
 * @since 2025-01-08
 */
@RestController
@RequestMapping("/job-task-distribution")
@AllArgsConstructor
public class JobTaskDistributionController extends BaseController {
    
    private final IJobTaskDistributionService jobTaskDistributionService;

    /**
     * 分页查询作业任务分布列表
     *
     * @param queryBo
     * @return
     * @folder 作业任务分布/分页查询作业任务分布列表
     */
    @GetMapping("/list")
    public TableDataInfo selectJobTaskDistributionPageList(JobTaskDistributionQueryBo queryBo) {
        startPage();
        TableVo<JobTaskDistribution, JobTaskDistributionPageVo> data = jobTaskDistributionService.selectJobTaskDistributionPageList(queryBo);
        return getDataTable(data);
    }

    /**
     * 查询作业任务分布详情信息
     *
     * @param jobTaskDistributionId
     * @return
     * @folder 作业任务分布/查询作业任务分布详情信息
     */
    @GetMapping("/{jobTaskDistributionId}")
    public AjaxResult getJobTaskDistributionInfo(@PathVariable("jobTaskDistributionId") Long jobTaskDistributionId) {
        JobTaskDistributionVo jobTaskDistributionVo = jobTaskDistributionService.selectJobTaskDistributionById(jobTaskDistributionId);
        if (ObjectUtil.isEmpty(jobTaskDistributionVo)) {
            return error("数据不存在!");
        }
        return success(jobTaskDistributionVo);
    }

    /**
     * 作业任务分布
     *
     * @param bo
     * @return
     * @folder 作业任务分布/新增作业任务分布
     */
    @Log(title = "作业任务分布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addJobTaskDistribution(@RequestBody @Validated(AddGroup.class) JobTaskDistributionBo bo) {
        return toAjax(jobTaskDistributionService.addJobTaskDistribution(bo));
    }

    /**
     * 编辑作业任务分布
     *
     * @param bo
     * @return
     * @folder 作业任务分布/编辑作业任务分布
     */
    @PostMapping("/edit")
    @Log(title = "作业任务分布", businessType = BusinessType.UPDATE)
    public AjaxResult updateJobTaskDistribution(@RequestBody @Validated(EditGroup.class) JobTaskDistributionBo bo) {

        return toAjax(jobTaskDistributionService.updateJobTaskDistribution(bo));
    }

    /**
     * 删除作业任务分布
     *
     * @param idList
     * @return
     * @folder 作业任务分布/删除作业任务分布
     */
    @Log(title = "作业任务分布", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> idList) {
        return toAjax(jobTaskDistributionService.removeJobTaskDistribution(idList));
    }

    /**
     * 下载作业任务分布模版
     *
     * @param response
     * @folder 作业任务分布/下载作业任务分布模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<JobTaskDistributionImportVo> util = new ExcelUtil<>(JobTaskDistributionImportVo.class);
        util.importTemplateExcel(response, "作业任务分布导入模版");
    }

    /**
     * 导入作业任务分布数据
     *
     * @param file
     * @return
     * @throws Exception
     * @folder 作业任务分布/导入作业任务分布数据
     */
    @Log(title = "作业任务分布", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<JobTaskDistributionImportVo> util = new ExcelUtil<>(JobTaskDistributionImportVo.class);
        List<JobTaskDistributionImportVo> voList = util.importExcel(file.getInputStream());
        jobTaskDistributionService.importJobTaskDistribution(voList);
        return success();
    }

}
